package com.jianyun.jyzs.model;

import com.jianyun.jyzs.bean.EngProjectLocResult;
import com.jianyun.jyzs.dao.EngLocationDao;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.IP2FragModel;

import java.util.HashMap;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/5/31.
 */

public class P2FragmentModel implements IP2FragModel {
    private static P2FragmentModel model;

    private P2FragmentModel() {
    }

    public static P2FragmentModel getInstance() {
        if (model == null) {
            model = new P2FragmentModel();
        }
        return model;
    }

    @Override
    public void getP2dataToService(final EngLocationDao dao, HashMap<String, String> map, final OnGetP2DataListener onGetP2DataListener) {
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        api.getNewEngineerLoc(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngProjectLocResult>() {
                    @Override
                    protected void onResponse(EngProjectLocResult response) {
                        if (response.isResult()) {
                            List<EngProjectLocResult.EngTypeLocBean> list = response.getList();
                            for (EngProjectLocResult.EngTypeLocBean ee : list) {
                                dao.insert(ee);
                            }
                            onGetP2DataListener.onSuccess();
                        } else {
                            onGetP2DataListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onGetP2DataListener.onFailed(msg);
                    }
                });
    }
}
